<style type="text/css">
.scrollable td
{
 text-align: center;
 width: 75px;
 padding: 5px;
 border-bottom-width: 1px;
 border-bottom-color: #ccc;
 border-bottom-style: solid;
}

br.clear
{
 clear: left;
}

table
{
 table-layout: fixed;
 border-collapse: collapse;
 empty-cells: show;
}

.scrollable
{
 float: left;
 display: inline-block;
 margin: 0px;
 padding: 2px;
 
 overflow: auto;
 overflow-x: hidden;
 width: auto;

 border-style: hidden;
}

.title
{
 color:#3B5998;
 background-color:#d8dfea;
 border-top: 1px solid #3B5998;
 height: auto;
 font-weight: bold;
 margin-bottom: 2px;
 padding: 1px;
 width: 425px
}

.box
{
 float: left;
 height: auto;
 margin: 3px;
 padding: 0px;
 
 border-style: hidden;
}

td.mine
{
 font-weight: bold;
 background-color: rgb(255, 255, 191);
}
</style>

<?php
include 'facebook.php';
include 'appinclude.php';
include 'schedule.inc.php';

// set up facebook api
$facebook = new Facebook($GLOBALS["api_key"], $GLOBALS["api_secret"]);
$facebook->require_frame();
$user = $facebook->require_login();

// establish link with mysql database
$link = @mysql_connect($GLOBALS["db_host"], $GLOBALS["db_user"], $GLOBALS["db_pass"]);
@mysql_select_db($GLOBALS["db_name"]);

//semester
$sem = $GLOBALS["semester"];
if(isset($_GET['sem']))
 $sem = addslashes($_GET['sem']);

// check if calendar in db
$cal_res = mysql_query("SELECT cal_string from cal_strings WHERE uid=$user AND semester='$sem'");
?>
<?php if(mysql_num_rows($cal_res) == 0): ?>
<fb:redirect url="<?php echo($GLOBALS['full_canvas']);?>/index.php" />
<?php else: ?>

<fb:header>CMU Schedules</fb:header>
<fb:tabs>
    <fb:tab-item href='index.php' title='Home' selected='false'/>
    <fb:tab-item href='classmates.php' title='Classmates' selected='true'/>
    <fb:tab-item href='settings.php' title='Settings' selected='false'/>
    <fb:tab-item href='invite.php' title='Invite Friends' selected='false'/>
</fb:tabs>
<br /><br />
<?php endif; ?>
<?php

//show all of your classmates - rough...
$freqData = array();
$someClassmates = false;
$res2 = mysql_query("SELECT Course, Description FROM Student_Class, Class WHERE Student_Class.semester='$sem' AND Student_Class.Class_ID = Class.ID AND uid='$user'");
while($srow = mysql_fetch_array($res2, MYSQL_ASSOC))
{
	//list all students in this class
	$written = 0;
	$course = courseName($srow['Course']);
	if($course === NULL)
	 continue;
	$mySection = getSection($srow['Course']);
	
	$res3 = mysql_query("SELECT uid, Course FROM Student_Class, Class WHERE Student_Class.Class_ID = Class.ID AND Student_Class.semester = '$sem' AND Class.Course LIKE '%$course%'");
	while($srow2 = mysql_fetch_array($res3, MYSQL_ASSOC))
	{
	 //write the class name only if we have any classmates
	 $stuid = $srow2['uid'];
	 if($stuid != $user)
	 {
	  $someClassmates = true;
	  if($written == 0)
	   echo "<div class='box'><div class='title'>".$srow['Description']." (".$course.")</div><div class='scrollable'><table>";
	  else if($written % 5 == 0)
	   echo '</tr><tr>';
	  
	  //a student we share a class with
	  $section = getSection($srow2['Course']);
	  if($mySection == $section)
	   echo "<td class='mine'><fb:profile-pic uid=".$stuid." size='square' /><br /><fb:name uid=".$stuid." linked='true' /><br />Section $section</td>";
	  else
	   echo "<td><fb:profile-pic uid=".$stuid." size='square' /><br /><fb:name uid=".$stuid." linked='true' /><br />Section $section</td>";
	  $written++;
	 
	  //increment the frequency count
	  array_key_exists($stuid, $freqData) ? $freqData[$stuid]++ : $freqData[$stuid] = 1;
	 }
	}
	if($written > 0)
	{
	 if(($written % 5) != 0)
	  for($i = 0; $i < (5 - ($written % 5)); $i++)
	   echo "<td></td>";
	 echo "</tr></table></div></div><br class='clear'><br>";
	}
}

if(!$someClassmates)
	echo "You don't share a class with any of our members for the semester $sem. You could <a href='invite.php'>invite someone.</a>";
else
{
	echo "<h3>You share most classes with:</h3><ol>";
	$maxFriends = 10;
	$count = 0;
	arsort($freqData);
	foreach($freqData as $fid => $num)
	{
	 if($count == $maxFriends)
	    break;
	 echo "<li><fb:name uid=".$fid." linked='true' /> (".$num.")";
	 $count++;
	}
	echo "</ol>";
}

mysql_close($link);
?>